<template>
	<div class="container add_edit">
		<el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
			<el-form-item label="区域">
				<el-cascader v-model="form.parent_code" show-all-levels :options="areaOptions" filterable
					ref="cascaderObj"
					:props="{ checkStrictly: true, emitPath: false, expandTrigger: 'hover', value: 'code', label: 'name' }"
					clearable @change="areaChange"></el-cascader>
			</el-form-item>
			<el-form-item label="名称" prop="name" ref="name">
				<el-input v-model="form.name" placeholder="请输入名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="拼音" prop="pinyin" ref="pinyin">
				<el-input v-model="form.pinyin" placeholder="请输入拼音" clearable></el-input>
			</el-form-item>
			<el-form-item label="类型">
				<el-select class="sele" v-model="form.type" disabled>
					<el-option v-for="item in typeOptions" :key="item.value" :label="item.name" :value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="拼音首字母" prop="first_letter" ref="first_letter">
				<el-input v-model="form.first_letter" placeholder="请输入拼音首字母" clearable></el-input>
			</el-form-item>
			<el-form-item label="编码" prop="code" ref="code">
				<el-input type="number" v-model="form.code" :min="0" placeholder="请输入编码" clearable
					:disabled="dataId ? true : false"></el-input>
				<div>
					<el-link class="remark_txt" type="info" href="http://www.mca.gov.cn/article/sj/xzqh/1980/"
						target="_blank">请参照全国行政区划代码填写 http://www.mca.gov.cn/article/sj/xzqh/1980/</el-link>
				</div>
			</el-form-item>
			<el-form-item label="邮政编码" prop="zip_code" ref="zip_code">
				<el-input type="number" v-model.number="form.zip_code" :min="0" placeholder="请输入邮政编码" clearable>
				</el-input>
			</el-form-item>
			<el-form-item>
				<el-button :loading="formLoading" round icon="el-icon-check" class="confirm-btn"
					@click="submitForm('ruleForm')">提交</el-button>
				<el-button round icon="el-icon-back" class="return-btn" @click="returnPage">返回</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	const __name = 'opendb-city-china';
	export default {
		data() {
			return {
				dataId: '',
				formLoading: false,
				areas: [],
				areaOptions: [],
				typeOptions: [{
						value: 0,
						name: '省'
					},
					{
						value: 1,
						name: '市'
					},
					{
						value: 2,
						name: '区'
					}
				],
				form: {
					parent_code: '',
					name: '',
					pinyin: '',
					type: 0,
					first_letter: '',
					code: '',
					zip_code: ''
				},
				rules: {
					name: [{
						required: true,
						message: '请输入名称',
						trigger: 'change'
					}],
					pinyin: [{
						required: true,
						message: '请输入拼音',
						trigger: 'change'
					}],
					first_letter: [{
						required: true,
						message: '请输入拼音首字母',
						trigger: 'change'
					}],
					code: [{
						required: true,
						message: '请输入编码',
						trigger: 'change'
					}],
					zip_code: [{
						required: true,
						message: '请输入邮政编码',
						trigger: 'change'
					}]
				}
			};
		},
		methods: {
			async loadData() {
				this.$db[__name]
					.noshopid()
					.noaction()
					.where('type < 2')
					.totree({
						orderby: 'code asc',
						loading: false
					})
					.then(res => {
						if (res.code == 200) {
							this.setChildren(res.datas);
							this.areaOptions = res.datas;
						}
					});

				if (!this.dataId) {
					return;
				}

				await this.$db[__name]
					.noshopid()
					.noaction()
					.tofirst(this.dataId).then(res => {
						if (res.code == 200) {
							for (let item in this.form) {
								this.form[item] = res.datas[item];
							}
						}
					});
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid, obj) => {
					// 默认获取第一个未验证 form 属性名
					this.$api.set_unvalidated_form_focus(this, obj);
					if (valid) {
						this.formLoading = true;
						if (!this.dataId) {
							this.$db[__name]
								.noshopid()
								.noaction()
								.add(this.form).then(res => {
									if (res.code == 200) {
										this.getOpenerEventChannel().emit('loadData');
										this.returnPage();
									}
									this.formLoading = false;
								}).catch(() => {
									this.formLoading = false;
								})
						} else {
							this.$db[__name]
								.noshopid()
								.noaction()
								.update(this.dataId, this.form).then(res => {
									if (res.code == 200) {
										this.getOpenerEventChannel().emit('loadData');
										this.returnPage();
									}
									this.formLoading = false;
								}).catch(() => {
									this.formLoading = false;
								})
						}
					}
				});
			},
			areaChange(res) {
				let area = this.areas.find(x => x.code == res);
				if (area) {
					this.form.type = area.type + 1;
				}
			},
			setChildren(res) {
				res.forEach(item => {
					this.areas.push(item);
					if (item.children && item.children.length <= 0) {
						item.children = null;
						return;
					}

					this.setChildren(item.children);
				});
			},
			returnPage() {
				uni.$emit('closeTab');
				uni.redirectTo({
					url: '/pages/system_set/area/area_manage'
				});
			}
		},
		created() {
			this.dataId = this.$route.query.id || '';
			this.loadData();
		}
	};
</script>

<style></style>
